package com.tianpengtech.admin.fair;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.admin.dictionary.DictionaryDao;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairauth.FairAuth;
import com.tianpengtech.common.model.feedback.FeedBack;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.XxxUtil;

/**
* 路由配置地址：add("/admin/fair", FairAdminController.class, "/admin/fair");
* 数据库映射地址：arp.addMapping(Fair.dao.tableName(),Fair.class);
*/
@Before(InitInterceptor.class)
@RequiresAuthentication
public class FairAdminController extends AbstractAdminController{

	private IAdminOpt opt = new FairAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=Fair.class;
		this.service=new FairAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {
		DictionaryDao dictionaryDao = new DictionaryDao();
		setAttr("region", Db.findByCache(Fair.FAIR_CACHE_NAME,Fair.FAIR_CACHE_REGION,"select REGION_ID,REGION_NAME from region where PARENT_ID=1"));
        setAttr("platform",dictionaryDao.readParents(10055));
        setAttr("channel",dictionaryDao.readParents(10056));
	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

		String[] channels = getParaValues("fair."+Fair.CHANNEL);
		String[] platforms = getParaValues("fair."+Fair.PLATFORM);
		model.set(Fair.CHANNEL,XxxUtil.arr2str(channels)); 
		model.set(Fair.PLATFORM,XxxUtil.arr2str(platforms)); 
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

		DictionaryDao dictionaryDao = new DictionaryDao();
		setAttr("region", Db.findByCache(Fair.FAIR_CACHE_NAME,Fair.FAIR_CACHE_REGION,"select REGION_ID,REGION_NAME from region where PARENT_ID=1"));
        setAttr("platform",dictionaryDao.readParents(10055));
        setAttr("channel",dictionaryDao.readParents(10056));
        setAttr("citys",Db.findByCache(Fair.FAIR_CACHE_NAME,Fair.FAIR_CACHE_REGION+model.getStr(Fair.PROVINCE),"select REGION_ID,REGION_NAME from region where PARENT_ID=?",model.getStr(Fair.PROVINCE)));
	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {
		String[] channels = getParaValues("fair."+Fair.CHANNEL);
		String[] platforms = getParaValues("fair."+Fair.PLATFORM);
		model.set(Fair.CHANNEL,XxxUtil.arr2str(channels)); 
		model.set(Fair.PLATFORM,XxxUtil.arr2str(platforms)); 
	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	
	
	/**
	 * 审核通过
	 */
	public void  pass(){
		int id = getParaToInt(Fair.ID,0);
		try {
			opt.changeStatus(getModel(Fair.class), id, Fair.STATUS_SUCCESS, Kv.create());
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	
	/**
	 * 拒绝理由填写页面
	 */
	public void  toRefuse(){
		int id = getParaToInt(Fair.ID,0);
		setAttr("fair",this.service.queryById(getModel(Fair.class), id));
		renderTemplate("fair-reason.html");
	}
	
	/**
	 * 审核不通过
	 */
	public void  unpass(){
		Fair fair = getModel(Fair.class);
		String content = getPara(Opertion.CONTENT);
		Kv kv = Kv.by(Opertion.CONTENT, content);
		try {
			opt.changeStatus(fair, fair.getLong(Fair.ID), Fair.STATUS_FAILURE, kv);
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}

	@Override
	public void parsePageRecord(Object results) {

		Fair fair = (Fair)results;
		fair.set(Fair.STATUS, fair.status());
		fair.set(Fair.AGE, fair.age());
	}
	
	
	public void showDetail(){
		int id = getParaToInt(Fair.ID, GlobalConfigConstant.DEFAULT_VALUE_ZERO);
		setAttr("fair", this.service.queryById(getModel(Fair.class), id));
		renderTemplate("fair-detail.html");
	}
}

	